package com.dun.re.controller;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.dun.re.service.ExpenditureService;
import com.dun.re.service.LoginService;
import com.dun.re.vo.Expenditure;
import com.dun.re.vo.User;

@Controller
@RequestMapping(value = "/expenditures")
@SessionAttributes("user")
public class ExpendituresController {

	@Autowired
	private ExpenditureService expenditureService;
	@Autowired
	private LoginService loginService;

	/**
	 * ModelAttribute provides Expenditure Object for new request
	 */
	@ModelAttribute
	public Expenditure newExpenditureRequest() {
		return new Expenditure();
	}

	/**
	 * loading expenditures page
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/expendituresPageLoad.htm", method = RequestMethod.GET)
	public String expendituresPageLoad(Model model) {
		List<String> userNamesList = new ArrayList<String>();

		try {
			if (loginService.getUsers().size() > 0) {
				for (User user : loginService.getUsers()) {
					String name = user.getUserName();
					userNamesList.add(name);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		model.addAttribute("userNamesList", userNamesList);
		return "addExpenditure";
	}

	/**
	 * saving new expenditure details into DB
	 * 
	 * @param validExpenditure
	 * @param expenditure
	 * @param result
	 * @param status
	 * @return
	 */
	@RequestMapping(value = "/addExpenditure.htm", method = RequestMethod.POST)
	public String addExpenditures(@Valid Expenditure expenditure,
			BindingResult result, SessionStatus status, Model model) {
		int count = 0;
		if (result.hasErrors()) {
			return "addExpenditure";
		} else {
			try {
				count = expenditureService.save(expenditure);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (count > 1) {
				model.addAttribute("success",
						"expenditure details saved into the DB");
				clearFormFields(expenditure);
				return "addExpenditure";
			} else
				return "home";
		}

	}

	/**
	 * clears the add expenditure form values
	 * 
	 * @param expenditure
	 */
	private void clearFormFields(Expenditure expenditure) {
		expenditure.setByWhom("");
		expenditure.setDescription("");
		expenditure.setOnWhom("Select One");
		expenditure.setAmount("");
		expenditure.setSpentDate("");
	}

	/**
	 * Display all expenditure details from database
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/mySpendings.htm", method = RequestMethod.GET)
	public String mySpendings(Model model) {
		try {
			/*Map<String, Object> model = new HashMap<String, Object>();
		    model.put("expenditureList",  expenditureService.getExpenditures());
*/
			List<Expenditure> expenditureList = expenditureService
			.getExpenditures();
		model.addAttribute("expenditureList", expenditureList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "spendings";

	}
	
	/**
	 * Methods retrieves the expenditures made by all users on the logged in User using the username of the logged in user
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/spendingsOnMe.htm")
	public String spendingsOnMe(Model model,@ModelAttribute User user) {
		String loggedInUser = user.getUserName();
			List<Expenditure> expenditureList = expenditureService
			.getExpenditures(loggedInUser);
		model.addAttribute("spendingsOnMe", expenditureList);
		return "spendings";

	}
	
}
